package com.cloudfast.platform.oauthclient;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.date.DateUtils;
import com.cloudfast.common.encode.EncodeUtils;
import com.cloudfast.common.encode.Md5Encode;
import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.controller.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 认证客户端管理
 *
 * @author liuyw
 * @date 2022年9月11日
 */
@RestController
@RequestMapping("oauthclient")
@Api(value = "客户端授权管理", tags = "客户端授权管理")
public class OauthClientController extends BaseController<OauthClientDetail> {
    @Autowired
    private OauthService oauthService;

    @PostMapping(value = "checkClientIdExist")
    @ApiOperation("判断clientid是否存在")
    @ApiImplicitParams({@ApiImplicitParam(name = "clientId", value = "客户端id", required = true),
            @ApiImplicitParam(name = "id", value = "主键", required = false)})
    public ApiResponse checkClientIdExist(String clientId, Long id) {
        return ApiResponse.success(oauthService.checkClientIdExist(clientId, id));
    }

    @PostMapping(value = "createSecret")
    @ApiOperation("创建app密钥")
    @ApiImplicitParams({@ApiImplicitParam(name = "clientId", value = "客户端id", required = true)})
    public ApiResponse createSecret(String clientId) {
        Map<String, String> map = new HashMap<>();
        String appSecret = Md5Encode.appSecret(clientId + DateUtils.getDate("yyyyMMddHHMMss"));
        map.put("appSecret", appSecret);
        map.put("clientSecret", EncodeUtils.encode(appSecret));
        return ApiResponse.success(map);
    }
}
